package com.java.concurrency.db;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DBConnection { 
	 
    private static DataSource dataSource; 
    private static final String DRIVER_NAME; 
    private static final String URL; 
    private static final String UNAME; 
    private static final String PWD; 
 
    static { 
 
        final ResourceBundle config = ResourceBundle 
                        .getBundle("database"); 
        DRIVER_NAME = config.getString("driverName"); 
        URL = config.getString("url"); 
        UNAME = config.getString("uname"); 
        PWD = config.getString("pwd"); 
 
        dataSource = setupDataSource(); 
    } 
 
    public static Connection getConnection() throws SQLException { 
        return dataSource.getConnection(); 
    } 
 
    private static DataSource setupDataSource() { 
        ComboPooledDataSource cpds = new ComboPooledDataSource(); 
        try { 
                cpds.setDriverClass(DRIVER_NAME); 
        } catch (PropertyVetoException e) { 
                e.printStackTrace(); 
        } 
        cpds.setJdbcUrl(URL); 
        cpds.setUser(UNAME); 
        cpds.setPassword(PWD); 
        cpds.setMinPoolSize(5); 
        cpds.setAcquireIncrement(5); 
        cpds.setMaxPoolSize(20); 
        return cpds; 
    } 
} 
